﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1.ConfigMsg
{
    public partial class FormConfigMsg : Form
    {
        MONITOR_DBDataContext sms = new MONITOR_DBDataContext();
        public FormConfigMsg()
        {
            InitializeComponent();
        }

        private void RefreshAcked_status_column()
        {
            
                for (int i = 0; i < this.sMS_SendDataGridView.RowCount; i++)
                {
                    if (this.sMS_SendDataGridView.Rows[i].Cells["Acked"].Value != null)
                    {
                        int value = Convert.ToUInt16(this.sMS_SendDataGridView.Rows[i].Cells["Acked"].Value);

                        if (value == 0)
                        {
                            this.sMS_SendDataGridView.Rows[i].Cells["acked_status"].Value = "等待回复";
                        }

                        if (value == 1)
                        {
                            this.sMS_SendDataGridView.Rows[i].Cells["acked_status"].Value = "已回复";
                        }

                        if (value == 2)
                        {
                            this.sMS_SendDataGridView.Rows[i].Cells["acked_status"].Value = "超时";
                        }

                        if (value == 3)
                        {
                            this.sMS_SendDataGridView.Rows[i].Cells["acked_status"].Value = "再次发送";
                        }

                    }
                }
            
        }

        private void RefreshDisposed_status_column()
        {
            for (int i = 0; i < this.sMS_SendDataGridView.RowCount; i++)
            {
                if (this.sMS_SendDataGridView.Rows[i].Cells["Disposed"].Value != null)
                {
                    int value = Convert.ToUInt16(this.sMS_SendDataGridView.Rows[i].Cells["Disposed"].Value);
                    if (value == 0)
                    {
                        this.sMS_SendDataGridView.Rows[i].Cells["disposed_status"].Value = "未发送";
                    }

                    if (value == 1)
                    {
                        this.sMS_SendDataGridView.Rows[i].Cells["disposed_status"].Value = "发送成功";
                    }

                    if (value == 2)
                    {
                        this.sMS_SendDataGridView.Rows[i].Cells["disposed_status"].Value = "发送失败";
                    }

                    if (value == 3)
                    {
                        this.sMS_SendDataGridView.Rows[i].Cells["disposed_status"].Value = "超时";
                    }

                    if (value == 4)
                    {
                        this.sMS_SendDataGridView.Rows[i].Cells["disposed_status"].Value = "CRC错误";
                    }

                    if (value == 5)
                    {
                        this.sMS_SendDataGridView.Rows[i].Cells["disposed_status"].Value = "其他错误";
                    }

                    if (value == 6)
                    {
                        this.sMS_SendDataGridView.Rows[i].Cells["disposed_status"].Value = "无效信息";
                    }
                }
            }
        }

        private void FormConfigMsg_Load(object sender, EventArgs e)
        {
            try
            {
                this.sMS_SendBindingSource.DataSource = sms.SMS_Send;

                //刷新回复状态列
                RefreshAcked_status_column();

                //刷新发送状态列
                RefreshDisposed_status_column();
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton1_Click_1(object sender, EventArgs e)
        {
            //sms.Refresh(RefreshMode.OverwriteCurrentValues, sms.SMS_Send);
            sms.Dispose();
            sms = new MONITOR_DBDataContext();
            this.sMS_SendBindingSource.DataSource = sms.SMS_Send;
            this.sMS_SendDataGridView.Refresh();

            //刷新回复状态列
            RefreshAcked_status_column();

            //刷新发送状态列
            RefreshDisposed_status_column();
        }

        private void FormConfigMsg_FormClosing(object sender, FormClosingEventArgs e)
        {
            sms.Dispose();
        }

        private void sMS_SendDataGridView_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            //刷新回复状态列
            RefreshAcked_status_column();

            //刷新发送状态列
            RefreshDisposed_status_column();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime fromTime = this.dateTimePicker1.Value.Date;
            DateTime toTime   = this.dateTimePicker2.Value.Date;

            toTime = toTime.AddHours(23);
            toTime = toTime.AddMinutes(59);
            toTime = toTime.AddSeconds(59);

            var smMsg =
                from sm in sms.SMS_Send
                where sm.InsertTime >= fromTime && sm.InsertTime <= toTime
                select sm;

            this.sMS_SendBindingSource.DataSource = smMsg;

            this.sMS_SendDataGridView.Refresh();

            //刷新回复状态列
            RefreshAcked_status_column();

            //刷新发送状态列
            RefreshDisposed_status_column();
        }
    }
}
